<?php

namespace app\admin\controller\service\money;

use app\api\model\service\Shop;
use app\api\model\service\Skill;
use app\api\model\service\UserInfo;
use app\common\controller\Backend;
use think\Db;
use think\Exception;

/**
 * 提现
 *
 * @icon fa fa-circle-o
 */
class Withdraw extends Backend
{

    /**
     * Withdraw模型对象
     * @var \app\admin\model\service\money\Withdraw
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\service\money\Withdraw;
        $this->view->assign("typeList", $this->model->getTypeList());
        $this->view->assign("withdrawtypeList", $this->model->getWithdrawtypeList());
        $this->view->assign("stateList", $this->model->getStateList());
    }

    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if (false === $this->request->isAjax()) {
            return $this->view->fetch();
        }
        //如果发送的来源是 Selectpage，则转发到 Selectpage
        if ($this->request->request('keyField')) {
            return $this->selectpage();
        }
        [$where, $sort, $order, $offset, $limit] = $this->buildparams();
        $list = $this->model
            ->where($where)
            // ->where('type','<>',1)
            ->order($sort, $order)
            ->paginate($limit);
        foreach ($list as &$value)
        {
            $value->info = in_array($value->type,[0,1,3])?\app\admin\model\service\skill\Skill::where('user_id',$value->user_id)->field('id,name,mobile')->find():\app\admin\model\service\shop\Shop::where('user_id',$value->user_id)->field('id,name,leader_mobile as mobile')->find();
        }
        $result = ['total' => $list->total(), 'rows' => $list->items()];
        return json($result);
    }

    public function refuse($ids = null)
    {
        $row = $this->model->get($ids);
        if (!$row) {
            $this->error(__('No Results were found'));
        }
        $row->state != 0 && $this->error('请勿重复审核');
        if($this->request->isPost()){
            $params = $this->request->post('row/a');
            Db::startTrans();
            try{
                switch ($row->type)
                {
                    case 0:
                        UserInfo::money($row->num,$row->user_id,'提现拒绝余额退回');
                        break;
                    case 2:
                        UserInfo::shopUserMoney($row->num,$row->user_id,'提现拒绝余额退回');
                        break;
                    case 3:
                        Skill::money($row->num,$row->user_id,'提现拒绝保证金退回');
                        $skill = \app\api\model\service\Skill::where('user_id', $row->user_id)->field('user_id,id,ensure_price')->find();
                        \app\api\model\service\SkillEnsurePay::updateSkillEnsure($skill['user_id'],$skill['ensure_price']);
                        break;
                    case 4:
                        Shop::money($row->num,$row->user_id,'提现拒绝保证金退回');
                        $shop = \app\api\model\service\Shop::where('user_id', $row->user_id)->field('user_id,id,ensure_price')->find();
                        \app\api\model\service\ShopEnsurePay::updateShopEnsure($shop['user_id'],$shop['ensure_price']);
                        break;
                    case 5:
                        UserInfo::bonus($row->num,$row->user_id,'提现拒绝佣金退回');
                        break;
                    case 6:
                        UserInfo::skillBonus($row->num,$row->user_id,'提现拒绝佣金退回');;
                        break;
                }
                $row->allowField(true)->save(['state'=>-1,'note'=>$params['note'],'updatetime'=>time()]);
                Db::commit();
            } catch (Exception $e) {
                Db::rollback();
                $this->error('提现拒绝失败',$e->getMessage());
            }
            $this->success('提现已拒绝');
        }
        $this->view->assign("id", $ids);
        return $this->view->fetch();
    }

    public function agree($ids = null)
    {
        $row = $this->model->get($ids);
        if (!$row) {
            $this->error(__('No Results were found'));
        }
        $row->state != 0 && $this->error('请勿重复审核');
        Db::startTrans();
        try{
            $row->allowField(true)->save(['state'=>1,'updatetime'=>time()]);
            Db::commit();
        } catch (Exception $e) {
            Db::rollback();
            $this->error('提现通过失败',$e->getMessage());
        }
        $this->success('提现已通过');
    }

    public function payment($ids = null)
    {
        $row = $this->model->get($ids);
        if (!$row) {
            $this->error(__('No Results were found'));
        }
        $row->state == 2 && $this->error('请勿重复打款');
        if($this->request->isPost()){
            $params = $this->request->post('row/a');
            Db::startTrans();
            try{
                $row->allowField(true)->save(['state'=>2,'updatetime'=>time(),'images'=>$params['images']]);
                Db::commit();
            } catch (Exception $e) {
                Db::rollback();
                $this->error('转账异常',$e->getMessage());
            }
            $this->success('已转账');
        }
        $this->view->assign("id", $ids);
        return $this->view->fetch();
    }



}
